//
//  SCPhotoAlbumBottomView.m
//  PhotoAlbum
//
//  Created by Chao Sheng on 2021/12/3.
//

#import "SCPhotoAlbumBottomView.h"

@interface SCPhotoAlbumBottomView ()

@property (nonatomic, strong) UIToolbar *toolbar;

@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIButton *previewBtn;

@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation SCPhotoAlbumBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.toolbar];
    [self addSubview:self.bottomLine];
    [self addSubview:self.previewBtn];
    [self addSubview:self.sureBtn];
    
    [self.toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.previewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.centerY.equalTo(self).offset(-sc_BOTTOMPADDING_HEIGHT/2);
        make.width.height.equalTo(@40);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.centerY.equalTo(self).offset(-sc_BOTTOMPADDING_HEIGHT/2);
        make.width.equalTo(@50);
        make.height.equalTo(@30);
    }];
    
    self.sureBtn.enabled = NO;
    if (self.sureBtn.enabled) {
        self.sureBtn.backgroundColor = sc_Blue;
    } else {
        self.sureBtn.backgroundColor = [sc_Blue colorWithAlphaComponent:0.5];
    }
}

- (void)setSelectedCount:(NSInteger)selectedCount {
    _selectedCount = selectedCount;
    if (_selectedCount>0) {
        NSString *title = [NSString stringWithFormat:@"完成(%ld/%ld)",_selectedCount,_maxCount];
        [self.sureBtn setTitle:title forState:UIControlStateNormal];
        [self.sureBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@80);
        }];
        self.sureBtn.enabled = YES;
        self.previewBtn.enabled = YES;
    } else {
        [self.sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self.sureBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@50);
        }];
        self.sureBtn.enabled = NO;
        self.previewBtn.enabled = NO;
    }
    [self updateState];
}

- (void)updateState {
    if (self.sureBtn.enabled) {
        self.sureBtn.backgroundColor = sc_Blue;
    } else {
        self.sureBtn.backgroundColor = [sc_Blue colorWithAlphaComponent:0.5];
    }
}

#pragma mark - 点击事件
- (void)previewAction {
    if (self.scBottomPreview) {
        self.scBottomPreview();
    }
}

- (void)compltion {
    if (self.scBottomCompltion) {
        self.scBottomCompltion();
    }
}

#pragma mark - 懒加载
- (UIToolbar *)toolbar {
    if (!_toolbar) {
        _toolbar = [[UIToolbar alloc] init];
        _toolbar.translucent = true;
        _toolbar.clipsToBounds = true;
    }
    return  _toolbar;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    }
    return _bottomLine;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor.whiteColor colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
        _sureBtn.backgroundColor = sc_Blue;
        _sureBtn.layer.cornerRadius = 3;
        _sureBtn.layer.masksToBounds = true;
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_sureBtn addTarget:self action:@selector(compltion) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)previewBtn {
    if (!_previewBtn) {
        _previewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_previewBtn setTitle:@"预览" forState:UIControlStateNormal];
        [_previewBtn setTitleColor:sc_Blue forState:UIControlStateNormal];
        [_previewBtn setTitleColor:[sc_Blue colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
        _previewBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _previewBtn.enabled = NO;
        [_previewBtn addTarget:self action:@selector(previewAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _previewBtn;
}


@end
